package com.banciyuan.bcywebview.base.helper;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

/**
 * Created by hfxt2008 on 14-8-22.
 * 用于管理detail的Progressbar的管理类，几乎所有页面都有这个组件
 * 这个组件是页面的loading页面，loadfail页面，nodata页面的UI组件和事件监听的统一管理
 */
public class ProgressbarHelper implements OnClickListener {

    public static final int PROGRESS_STATE_SUCCESS = 1001;
    public static final int PROGRESS_STATE_FAILED = 1002;
    public static final int PROGRESS_STATE_LOADING = 1003;
    public static final int PROGRESS_STATE_NODATA = 1004;

    private LinearLayout mLoadingLayout;
    private LinearLayout mFailedLayout;
    private LinearLayout mNodataLayout;
    private LinearLayout mCustomLayout;
    private TextView mLoadingTv;
    private TextView mNodataTv;
    private TextView mFailedTv;

    private OnProgressCallbacks mCallbacks;

    private int stateNumber = PROGRESS_STATE_SUCCESS;

    public ProgressbarHelper(View mProgressbarView) {

        mLoadingLayout = (LinearLayout) mProgressbarView.findViewById(R.id.progressbar_loading_layout);
        mFailedLayout = (LinearLayout) mProgressbarView.findViewById(R.id.progressbar_failed_layout);
        mNodataLayout = (LinearLayout) mProgressbarView.findViewById(R.id.progressbar_nodata_layout);
        mFailedTv = (TextView) mProgressbarView.findViewById(R.id.progressbar_failed_tv);
        mLoadingTv = (TextView) mProgressbarView.findViewById(R.id.progressbar_loading_tv);
        mNodataTv = (TextView) mProgressbarView.findViewById(R.id.progressbar_nodata_tv);
        mCustomLayout = (LinearLayout) mProgressbarView.findViewById(R.id.custom_container);

        mFailedLayout.setOnClickListener(this);
        mFailedTv.setOnClickListener(this);
        mNodataLayout.setOnClickListener(this);
        mNodataTv.setOnClickListener(this);
    }

    public void onFailed() {
        stateNumber = PROGRESS_STATE_FAILED;
        mLoadingLayout.setVisibility(View.GONE);
        mNodataLayout.setVisibility(View.GONE);
        mFailedLayout.setVisibility(View.VISIBLE);
    }

    public void onFailed(String onfailedTx) {
        if (getState() != PROGRESS_STATE_LOADING) {
            stateNumber = PROGRESS_STATE_FAILED;
            mFailedTv.setText(onfailedTx);
        } else {
            stateNumber = PROGRESS_STATE_FAILED;
            mLoadingLayout.setVisibility(View.GONE);
            mNodataLayout.setVisibility(View.GONE);
            mFailedLayout.setVisibility(View.VISIBLE);
            mFailedTv.setText(onfailedTx);
        }
    }


    public void onLoading() {
        stateNumber = PROGRESS_STATE_LOADING;
        mFailedLayout.setVisibility(View.GONE);
        mNodataLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.VISIBLE);
    }

    public void onNodata() {
        if (getState() != PROGRESS_STATE_LOADING)
            stateNumber = PROGRESS_STATE_NODATA;
        else {
            stateNumber = PROGRESS_STATE_NODATA;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            mNodataLayout.setVisibility(View.VISIBLE);
        }
    }

    public void onNodata(String nodataTx) {
        if (getState() != PROGRESS_STATE_LOADING) {
            stateNumber = PROGRESS_STATE_NODATA;
            mNodataTv.setText(nodataTx);
        } else {
            stateNumber = PROGRESS_STATE_NODATA;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            mNodataLayout.setVisibility(View.VISIBLE);
            mNodataTv.setText(nodataTx);
        }
    }

    public void onNodata(String nodataTx, boolean setNoDataVisible) {
        if (setNoDataVisible) {
            stateNumber = PROGRESS_STATE_NODATA;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            mNodataLayout.setVisibility(View.VISIBLE);
            mNodataTv.setText(nodataTx);
        }
    }

    public void onSuccess() {
        if (getState() != PROGRESS_STATE_SUCCESS) {
            stateNumber = PROGRESS_STATE_SUCCESS;
            mFailedLayout.setVisibility(View.GONE);
            mLoadingLayout.setVisibility(View.GONE);
            mNodataLayout.setVisibility(View.GONE);
        }
    }

    public void onCustom() {
        mFailedLayout.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.GONE);
        mNodataLayout.setVisibility(View.GONE);
        mCustomLayout.setVisibility(View.VISIBLE);
    }

    public LinearLayout getCustom() {
        return mCustomLayout;
    }

    public int getState() {
        return stateNumber;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.progressbar_failed_layout:
            case R.id.progressbar_nodata_layout:
            case R.id.progressbar_failed_tv:
            case R.id.progressbar_nodata_tv:
                callAction(v.getId());
                break;

            default:
                break;
        }
    }

    private void callAction(int id) {
        if (mCallbacks != null) {
            mCallbacks.onActionClick(id);
        }
    }

    /**
     * 设置progressbar 回调
     *
     * @param callbacks
     */
    public void setActionCallbacks(OnProgressCallbacks callbacks) {
        mCallbacks = callbacks;
    }

    /**
     * progressbar 回调
     *
     * @author hfxt2008
     */
    public interface OnProgressCallbacks {
        /**
         * progressbar中 通过addAction增加的点击回调
         *
         * @param id
         * @param position 如果增加是一个list,表示点击位置,否则全为0
         */
        void onActionClick(int id);
    }

}